package com.maiyuren.fishkit.ui.pages.favourite

import android.annotation.SuppressLint
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.ArrowBack
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModel
import androidx.navigation.NavHostController
import com.maiyuren.fishkit.history.FavouriteManager
import com.maiyuren.fishkit.ui.theme.DisableColor
import com.maiyuren.fishkit.ui.theme.lightGray
import com.maiyuren.fishkit.utils.FishAlertUtils
import com.maiyuren.fishkit.utils.FishEvent
import com.maiyuren.fishkit.utils.FishEventBus

private object vm: ViewModel() {
    var navController: NavHostController? = null

    val data: MutableList<FavouriteManager.GroupItem> = mutableStateListOf()

    fun initFavourite() {
        data.clear()
        FavouriteManager.instance().words_.forEach {
            data.add(it)
        }
    }
}


@OptIn(ExperimentalMaterial3Api::class)
@RequiresApi(Build.VERSION_CODES.Q)
@Composable
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter", "UnrememberedMutableState")
fun FavouritePage(navController: NavHostController) {
    vm.navController = navController
    vm.initFavourite()

    Scaffold(
        containerColor = lightGray,
        topBar = {
            Row(modifier = Modifier
                .fillMaxWidth()
                .background(color = MaterialTheme.colorScheme.background),
                verticalAlignment = Alignment.CenterVertically
            ) {
                IconButton(onClick = {
                    navController.popBackStack()
                }) {
                    Icon(
                        imageVector = Icons.Outlined.ArrowBack,
                        contentDescription = null,
                        tint = Color.Black
                    )
                }
                Text(text = "收藏本",
                    modifier = Modifier.weight(1.0f),
                    textAlign = TextAlign.Center)
                IconButton(onClick = { 
                    FishAlertUtils.confirm(title = "請確認是否要清除所有收藏本記錄嗎？") {
                        FavouriteManager.instance().clear();
                        vm.data.clear()
                        FishEventBus.emit(FishEvent.clearFavourite, null)
                    }
                }) {
                    Icon(
                        imageVector = Icons.Outlined.Delete,
                        contentDescription = null,
                        tint = Color.Black
                    )
                }
            }
        }
    ) {
        Column(modifier = Modifier.padding(it)) {
            LazyColumn {
                items(vm.data.size) { index ->
                    GroupItemUI(vm.data[index])
                }
            }
        }
    }
}

@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun GroupItemUI(item: FavouriteManager.GroupItem) {
    FlowRow(verticalAlignment = Alignment.CenterVertically) {
        Text(text = item.group ?: "",
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 4.dp, top = 4.dp),
            fontStyle = FontStyle.Italic,
            color = DisableColor
        )
        item.forEach {
            Text(text = it ?: "",
                modifier = Modifier
                    .padding(all = 8.dp)
                    .clickable {
                        FishEventBus.emit(FishEvent.goWord, it)
                        vm.navController?.popBackStack()
                    },
                fontSize = 22.sp
            )
        }
    }
}